/**
 * 
 */
package org.apache.lucene.analysis.thesaurus;

import java.io.*;
import java.util.ArrayList;

import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;

/**
 * @author Jolestar
 */
public class AnalyzerUtils {
	
	public static Token[] tokensFromAnalysis(Analyzer analyzer, String text)
			throws IOException {
		TokenStream stream = analyzer.tokenStream("contents", new StringReader(text));
		CharTermAttribute termAtt = (CharTermAttribute) stream
				.getAttribute(CharTermAttribute.class);
		TypeAttribute typeAtt = (TypeAttribute) stream
				.getAttribute(TypeAttribute.class);

		ArrayList<Token> tokenList = new ArrayList<Token>();

		while (stream.incrementToken()) {
			//termAtt.toString();
			//tokenList.add(termAtt);
		}
		/*
		 * while (true) { Token token = stream.next(); if (token == null) break;
		 * tokenList.add(token); }
		 */
		return (Token[]) tokenList.toArray(new Token[0]);
	}

	public static void displayTokens(Analyzer analyzer, String text)
			throws IOException {
		Token[] tokens = tokensFromAnalysis(analyzer, text);
		for (int i = 0; i < tokens.length; i++) {
			Token token = tokens[i];
			//System.out.print("[" + token.termText() + "] ");
		}
	}
}
